<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>部门新增</span>
        <el-button @click="backUserList" style="float: right; padding: 3px 0" type="text">返回员工列表</el-button>
      </div>
      <div>
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="部门名" prop="form.departmentName">
            <el-input style="width: 300px" v-model="form.departmentName"></el-input>
          </el-form-item>
          <el-form-item label="主管人员" prop="form.manager">
            <el-select v-model="form.manager" placeholder="请选择">
              <el-option
                      v-for="user in users"
                      :key="user.name"
                      :label="user.name"
                      :value="user.name">
                <span style="float: left">{{ user.name }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{ user.account }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <el-button @click="submit" type="primary">提交修改</el-button>
      </div>
    </el-card>

  </div>
</template>

<script>
  import {addDepartment} from "../../network/Department";
  import {getAllUser} from "../../network/UserInfo"

  export default {
    name: "DepartmentAdd",
    data() {
      return {
        departmentIds:[],
        form: {
          departmentName: '',
          manager: ''
        },
        users: []
      }
    },
    methods: {
      getAllUser() {
        getAllUser().then(res => {
          this.users =  res.data
          console.log(this.users)
        })
      },
      backUserList() {
        this.$router.back(1)
      },
      submit() {
        console.log(this.form)
        addDepartment(this.form).then(res => {
          this.$message.success('添加部门成功')
          setTimeout(() => {
            this.$router.back(1)
          }, 500)
        }, err => {
          this.$message.error('添加部门失败，请检查错误')
        })
      }
    },
    beforeMount() {
      this.getAllUser();
    }
  }
</script>

<style scoped>

</style>
